<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class CateHasRolesDB  extends DBReader{

    //添加 cate 拥有的 role 信息
    public function addCateHasRoles($cate_id,$role_id){

        $sql = "insert into cate_has_roles(cate_id,role_id) values(:cate_id,:role_id)";

        $bindarr = array(
            'cate_id'=>$cate_id,
            'role_id'=>$role_id,
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //删除 cate_id 对应的信息
    public function delByCateId($cate_id){
        $bindarr = array(
            'cate_id'=>$cate_id,
        );

        $sql = "delete from cate_has_roles where cate_id = :cate_id";
        $this->db()->execute($sql,$bindarr);
    }

    //删除 role_id 对应的信息
    public function delByRoleId($role_id){
        $bindarr = array(
            'role_id'=>$role_id,
        );

        $sql = "delete from cate_has_roles where role_id = :role_id";
        $this->db()->execute($sql,$bindarr);
    }

    //获取用户所有的权限分类ID
    public function getCateIdsByUserId($user_id)
    {
        $sql = "select cate_id from cate_has_roles cr left join user_has_roles ur on cr.role_id = ur.role_id where ur.user_id = :user_id";

        $rcate_ids = $this->db()->getOneAll($sql,array('user_id'=>$user_id));

        return $rcate_ids;
    }

    //获取角色id对应的cate 信息
    public function getCateHasRoles($role_id){

        $sql = 'select cate_id, role_id from cate_has_roles where role_id = :role_id';

        return $this->db()->getAll($sql,array('role_id'=>$role_id));
    }
}